﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Data.SqlClient;

namespace FiyatDegisikligi
{
    public partial class frmEtiket : XtraForm
    {
        public List<string> Stoklar { get; set; }

        public frmEtiket()
        {
            InitializeComponent();
        }

        private void DepoYukle()
        {
            cmbDepo.Properties.Items.Add("0-Genel");
            SqlDataReader rd = DataLayer.Get.Reader("SELECT     dep_no, dep_adi  FROM         DEPOLAR");
            while (rd.Read())
            {
                cmbDepo.Properties.Items.Add(rd.GetInt32(0).ToString() + "-" + rd.GetString(1));
            }
            rd.Close();
            cmbDepo.SelectedIndex = 0;
        }


        private int EldekiMiktar(string stokKodu, int Depo)
        {
            return DataLayer.Get.ExecScalar<int>(string.Format("select dbo.fn_DepodakiMiktar('{0}',{1},'2900-01-01')", stokKodu, Depo));
        }

        private void EtiketBas()
        {
            int Depo = Cevir.Parse<int>(cmbDepo.Text.Split('-')[0]);
            Dictionary<string, int> etiketler = new Dictionary<string, int>();
            if (!chkEldekiMiktar.Checked)
            {
                if (!string.IsNullOrEmpty(txtAdet.Text))
                {
                    int miktar;
                    miktar = Cevir.Parse<int>(txtAdet.Text);
                    foreach (string s in Stoklar)
                    {
                        if (!etiketler.ContainsKey(s))
                        {
                            etiketler.Add(s, miktar);
                        }
                    }
                }
            }
            else
            {
                int miktar;
                foreach (string s in Stoklar)
                {
                    miktar = EldekiMiktar(s, Depo);
                    if (!etiketler.ContainsKey(s))
                    {
                        etiketler.Add(s, miktar);
                    }
                }
            }

            Printing prn = new Printing();
            prn.Stoklar = etiketler;
            prn.EtiketAdi = cmbEtiketTipi.Text;
            prn.DepoNo = Depo;
            prn.Print();
        }


        private void frmEtiket_Load(object sender, EventArgs e)
        {
            DepoYukle();

            if (File.Exists(Application.StartupPath + "\\etiket_dizaynlari.txt"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\etiket_dizaynlari.txt"))
                {
                    string line = string.Empty;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        string[] alan = line.Split(';');
                        cmbEtiketTipi.Properties.Items.Add(alan[0]);
                    }
                    sr.Close();
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!chkEldekiMiktar.Checked && string.IsNullOrEmpty(txtAdet.Text))
            {
                MessageBox.Show("Miktar Girin !");
                return;
            }
            EtiketBas();
        }
    }
}
